﻿using Max.Ph5_2009A.NewShopping.Domain.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.Ph5_2009A.NewShopping.Domain
{
    /// <summary>
    /// 订单主表信息
    /// </summary>
    [Table("OrderMasterInfo")]
    public class OrderMasterInfoModel : AudtiField<Guid>
    {

        #region 收件人信息
        //冗余字段
        public string? RecivedName { get; set; }
        public string? RecivedPhone { get; set; }
        public string? RecivedAddress { get; set; }

        #endregion

        #region 订单信息

        /// <summary>
        /// 订单编号
        /// </summary>
        [Required(ErrorMessage = "订单编号不能为空")]
        public string OrderNo { get; set; }

        /// <summary>
        /// 会员ID（谁下的单）
        /// </summary>
        public Guid MemberId { get; set; }

        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal Total { get; set; }
        /// <summary>
        /// 下单时间
        /// </summary>
        public DateTime OrderTime { get; set; } = System.DateTime.UtcNow;


        /// <summary>
        /// 订单状态
        /// </summary>
        public OrderState OrderState { get; set; } = OrderState.ToBePay;

        /// <summary>
        /// 优惠金额
        /// </summary>
        public decimal DiscountMoney { get; set; } = 0;

        #endregion

        #region 支付信息
        /// <summary>
        /// 支付金额
        /// </summary>
        public decimal PayMoney { get; set; }

        /// <summary>
        /// 支付方式
        /// </summary>
        public OrderPayState PayType { get; set; } = OrderPayState.WeiXin;

        /// <summary>
        /// 支付时间
        /// </summary>
        public DateTime PayTime { get; set; }

        #endregion

        #region 不映射到数据库，只做显示用

        /// <summary>
        /// 下单时间
        /// </summary>
        [NotMapped]
        public string OrderTimeValue { get { return this.OrderTime.ToString("yyyy-MM-dd HH:mm:ss"); } }

        /// <summary>
        /// 订单状态
        /// </summary>
        [NotMapped]
        public string OrderStateValue
        {
            get
            {                
                var t = typeof(OrderState);
                string v = "";
                //var n =t.GetEnumNames().Where(x => x.Equals(this.OrderState.ToString())).FirstOrDefault();               

                t.GetEnumNames().ToList().ForEach(x =>
                {
                    if ((int)Enum.Parse(t, x) == (int)this.OrderState)
                    {
                        var f = t.GetField(x);
                        var obs = f.GetCustomAttributes(typeof(DisplayAttribute), true);
                        if (obs != null && obs.Length > 0)
                        {
                            v = ((DisplayAttribute)obs[0]).Description ?? x;
                        }
                        else { v = x; }
                    }
                });
                return v;
            }
        }

        #endregion
    }
}
